class SwaggerConfig:
    """
    Swagger配置类，用于生成Flasgger配置
    支持选择不同版本的Swagger UI风格
    """

    def __init__(
        self,
        title="API文档",
        description="API接口文档",
        version="1.0.0",
        contact_name=None,
        contact_email=None,
        ui_version="3.x",
        offline=False,
    ):
        """
        初始化Swagger配置

        参数:
            title: API文档标题
            description: API文档描述
            version: API版本
            contact_name: 联系人姓名
            contact_email: 联系人邮箱
            ui_version: Swagger UI版本，可选 "2.x" 或 "3.x"
            offline: 是否使用本地资源，可选 True 或 False
        """
        # 初始化Swagger配置参数
        # 参数:
        #   title: API文档标题
        #   description: API文档描述
        #   version: API版本号
        #   contact_name: 联系人姓名
        #   contact_email: 联系人邮箱
        #   ui_version: Swagger UI版本，可选 "2.x" 或 "3.x"
        #   offline: 是否使用本地资源，可选 True 或 False
        # 返回值: 无
        self.title = title
        self.description = description
        self.version = version
        self.contact_name = contact_name
        self.contact_email = contact_email
        self.ui_version = ui_version
        self.offline = offline

    def get_template(self):
        """获取Swagger模板配置"""
        template = {
            "info": {
                "title": self.title,
                "description": self.description,
                "version": self.version,
                "contact": None,
            }
        }

        # 添加联系人信息（如果提供）
        if self.contact_name or self.contact_email:
            template["info"]["contact"] = {}
            if self.contact_name:
                template["info"]["contact"]["name"] = self.contact_name
            if self.contact_email:
                template["info"]["contact"]["email"] = self.contact_email

        return template

    def get_config(self):
        """获取Swagger配置"""
        # 基础配置，适用于所有版本
        config = {
            "headers": [],
            "specs": [
                {
                    "endpoint": "apispec",
                    "route": "/apispec.json",
                    "rule_filter": lambda rule: True,
                    "model_filter": lambda tag: True,
                }
            ],
            "static_url_path": "/flasgger_static",
            "swagger_ui": True,
            "specs_route": "/apidocs/",
            # 添加以下配置以支持蓝图
            "merge_specs": True,
            "doc_expansion": "list",
        }

        # 根据UI版本选择不同的配置
        if self.ui_version.lower() == "2.x":
            # 老式Swagger UI (2.x)配置
            if self.offline:
                # 使用本地资源
                config.update(
                    {
                        "swagger_ui_bundle_js": "/static/swagger-ui/2.x/swagger-ui.js",
                        "swagger_ui_css": "/static/swagger-ui/2.x/swagger-ui.css",
                        "jquery_js": "/static/swagger-ui/2.x/jquery.min.js",
                        "swagger_ui_config": {
                            "docExpansion": "none",  # 默认折叠所有API
                            "apisSorter": "alpha",  # 按字母顺序排序API
                            "operationsSorter": "method",  # 按HTTP方法排序操作
                        },
                    }
                )
            else:
                # 使用CDN资源 - 使用更可靠的CDN
                config.update(
                    {
                        "swagger_ui_bundle_js": "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/2.2.10/swagger-ui.min.js",
                        "swagger_ui_css": "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/2.2.10/css/swagger-ui.min.css",
                        "jquery_js": "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js",
                        # 添加Handlebars依赖
                        "handlebars_js": "https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js",
                        "swagger_ui_config": {
                            "docExpansion": "none",
                            "apisSorter": "alpha",
                            "operationsSorter": "method",
                        },
                    }
                )
        else:
            # 现代Swagger UI (3.x)配置
            if self.offline:
                # 使用本地资源
                config.update(
                    {
                        "swagger_ui_bundle_js": "/static/swagger-ui/3.x/swagger-ui-bundle.js",
                        "swagger_ui_standalone_preset_js": "/static/swagger-ui/3.x/swagger-ui-standalone-preset.js",
                        "swagger_ui_css": "/static/swagger-ui/3.x/swagger-ui.css",
                        "swagger_ui_config": {
                            "deepLinking": True,
                            "layout": "BaseLayout",
                            "showExtensions": True,
                            "showCommonExtensions": True,
                        },
                    }
                )
            else:
                # 使用CDN资源 - 使用更可靠的CDN
                config.update(
                    {
                        "swagger_ui_bundle_js": "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.18.3/swagger-ui-bundle.min.js",
                        "swagger_ui_standalone_preset_js": "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.18.3/swagger-ui-standalone-preset.min.js",
                        "swagger_ui_css": "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.18.3/swagger-ui.min.css",
                        "swagger_ui_config": {
                            "deepLinking": True,
                            "layout": "BaseLayout",
                            "showExtensions": True,
                            "showCommonExtensions": True,
                        },
                    }
                )

        return config

    def init_swagger(self, app):
        """
        初始化Flask应用的Swagger

        参数:
            app: Flask应用实例

        返回:
            Swagger实例
        """
        from flasgger import Swagger

        swagger = Swagger(app, config=self.get_config(), template=self.get_template())

        return swagger
